package wechat.bean.request.message;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSONObject;

import wechat.resources.AppFilter;
import wechat.resources.InstanceResource;
import wechat.service.base.WechatApi;
import wechat.util.http.HttpClientUtil;

/**
 * 企业号
 * 
 */
public class TextBean extends ActiveMessageBaseBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private List<String> touser;
	private List<String> toparty;
	private List<String> totag;
	private String msgtype;
	private String agentid;
	private String text;
	private String safe;
	
	public TextBean(){
		this.msgtype = ActiveMessageBaseBean.Message_Text;
		this.safe = "0";
	}
	
	
	public List<String> getTouser() {
		return touser;
	}
	public void setTouser(List<String> touser) {
		this.touser = touser;
	}
	public List<String> getToparty() {
		return toparty;
	}
	public void setToparty(List<String> toparty) {
		this.toparty = toparty;
	}
	public List<String> getTotag() {
		return totag;
	}
	public void setTotag(List<String> totag) {
		this.totag = totag;
	}
	public String getMsgtype() {
		return msgtype;
	}
	public void setMsgtype(String msgtype) {
		this.msgtype = msgtype;
	}
	public String getAgentid() {
		return agentid;
	}
	public void setAgentid(String agentid) {
		this.agentid = agentid;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getSafe() {
		return safe;
	}
	public void setSafe(String safe) {
		this.safe = safe;
	}
	
	
	@Override
	public String toString() {
		return "TextBean [touser=" + touser + ", toparty=" + toparty + ", totag=" + totag + ", msgtype=" + msgtype
				+ ", agentid=" + agentid + ", text=" + text + ", safe=" + safe + "]";
	}
	
	public String toWechatString() throws Exception{
		JSONObject jsonObject = new JSONObject();
		if (this.touser == null || this.touser.size() <= 0) {
			throw new Exception("接受者不能为空"); 
		}
		if (this.agentid == null || this.agentid.equals("")) {
			throw new Exception("企业应用的id不能为空"); 
		}
		
		String touserVar = "";
		for (int i = 0; i < this.touser.size(); i++) {
			if (i == 0) {
				touserVar = this.touser.get(i);
			}
			else {
				touserVar = touserVar + "|" + this.touser.get(i);
			}
		}
		jsonObject.put("touser", touserVar);
		
		
		if (this.toparty != null && this.toparty.size() > 0) {
			String topartyVar = "";
			for (int i = 0; i < this.toparty.size(); i++) {
				if (i == 0) {
					topartyVar = this.toparty.get(i);
				}
				else {
					topartyVar = topartyVar + "|" + this.toparty.get(i);
				}
			}
			jsonObject.put("toparty", topartyVar);
		}

		
		if (this.totag != null && this.totag.size() > 0) {
			String totagVar = "";
			for (int i = 0; i < this.totag.size(); i++) {
				if (i == 0) {
					totagVar = this.totag.get(i);
				}
				else {
					totagVar = totagVar + "|" + this.totag.get(i);
				}
			}
			jsonObject.put("totag", totagVar);
		}

		JSONObject content = new JSONObject();
		content.put("content", this.text);
		
		jsonObject.put("msgtype", this.msgtype);
		jsonObject.put("agentid", this.agentid);
		jsonObject.put("text",content);
		jsonObject.put("safe", this.safe);
		
		return jsonObject.toJSONString();
	}
	
	
	public String toAllUserWechatString() throws Exception{
		JSONObject jsonObject = new JSONObject();
		if (this.agentid == null || this.agentid.equals("")) {
			throw new Exception("企业应用的id不能为空"); 
		}
		
		jsonObject.put("touser", "@all");
		
		if (this.toparty != null && this.toparty.size() > 0) {
			String topartyVar = "";
			for (int i = 0; i < this.toparty.size(); i++) {
				if (i == 0) {
					topartyVar = this.toparty.get(i);
				}
				else {
					topartyVar = topartyVar + "|" + this.toparty.get(i);
				}
			}
			jsonObject.put("toparty", topartyVar);
		}

		
		if (this.totag != null && this.totag.size() > 0) {
			String totagVar = "";
			for (int i = 0; i < this.totag.size(); i++) {
				if (i == 0) {
					totagVar = this.totag.get(i);
				}
				else {
					totagVar = totagVar + "|" + this.totag.get(i);
				}
			}
			jsonObject.put("totag", totagVar);
		}

		
		JSONObject content = new JSONObject();
		content.put("content", this.text);
		
		jsonObject.put("msgtype", this.msgtype);
		jsonObject.put("agentid", this.agentid);
		jsonObject.put("text",content);
		jsonObject.put("safe", this.safe);
		
		return jsonObject.toJSONString();
	}
	
	
	
	public static void main(String[] args) throws Exception {
		InstanceResource instanceResource = AppFilter.chooseApp("53");
		String access_token = "OHy3lmanmEAGPn8sYqDV3NjBo7ok7WNgE8pItQtZjfcTMH_nlaCQEJSxCmhHZAwu";
		String url = WechatApi.getSend_message(access_token);
		
		
		List<String> to = new ArrayList<String>();
		to.add("zhang");
		to.add("scrzxx");
		
		TextBean textBean = new TextBean();
		textBean.setAgentid("53");
		textBean.setText("你好，hello。哈哈");
		textBean.setTouser(to);
		String param = textBean.toWechatString();
		System.out.println(param);
		String result = HttpClientUtil.post(url, param);
		JSONObject jsonResult = JSONObject.parseObject(result);
		System.out.println(jsonResult);
	}

}
